package yxy.game.pm2.module.stairway;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.function.Function;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.player.FunctionHandle;

import java.util.ArrayList;

public class StairwayPlayerModule extends PlayerModule {
	// bean===========================================================================

	private String code;// 比赛代号 判断不同的代号更新
	private DateTime updateTime;

	private ArrayList<Integer> oppos = new ArrayList<>();// 对手池

	private int times; // 总挑战次数
	private int buytimes;// 总购买次数

	private int bestRank;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public DateTime getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(DateTime updateTime) {
		this.updateTime = updateTime;
	}

	public ArrayList<Integer> getOppos() {
		return oppos;
	}

	public void setOppos(ArrayList<Integer> oppos) {
		this.oppos = oppos;
	}

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public int getBuytimes() {
		return buytimes;
	}

	public void setBuytimes(int buytimes) {
		this.buytimes = buytimes;
	}

	public int getBestRank() {
		return bestRank;
	}

	public void setBestRank(int bestRank) {
		this.bestRank = bestRank;
	}

	// func===========================================================================

	private void update() {
		try {
			if (tryLock()) {
				PlayerBean playerBean = PlayerBean.get(getPlayerCode());
				ServerBean serverBean = ServerBean.get(playerBean.getServerId());
				AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

				StairwayModule module = StairwayModule.get(areaBean.getGroupId());
				boolean set = false;
				if (getCode() == null || !module.getCode().equals(getCode())) {
					setCode(module.getCode());
					StairwayRecord.clear(getPlayerCode());
					set = true;
				}
				DateTime now = DateTime.now();
				if (getUpdateTime() == null || !new LocalDate(now).equals(new LocalDate(getUpdateTime()))) {
					setUpdateTime(now);
					setTimes(0);
					setBuytimes(0);
					set = true;
				}
				if (set) {
					set();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			unlock();
		}
	}

	public int remainTimes() {
		int maxtimes = StairwayHandle.data().getNum();
		int remain = maxtimes - getTimes();
		return remain;
	}

	public int remainBuyTimes() {
		int maxtimes = StairwayHandle.data().getBuynum();
		int remain = maxtimes - getBuytimes();
		return remain;
	}

	// redis==========================================================================

	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:STAIRWAY", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public StairwayPlayerModule get(String playerCode) {
		try {
			StairwayPlayerModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), StairwayPlayerModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				if (!new FunctionHandle().check(Function.天梯争霸, playerCode)) {
					return null;
				}
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private StairwayPlayerModule create(String playerCode) {
		StairwayPlayerModule module = new StairwayPlayerModule();
		module.setPlayerCode(playerCode);
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				StairwayPlayerModule stairwayPlayerModule = getv(key(playerCode));
				if(stairwayPlayerModule != null)MySQLCacheDAO.setV(key, stairwayPlayerModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
